package com.cai.dao.impl;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.cai.dao.DbUtil;
import com.cai.dao.DetailedUserDao;
import com.cai.pojo.DetailedUser;

import java.util.List;

/**
 * @author RuoLi
 */
public class DetailedUserDaoImpl implements DetailedUserDao {
    DbUtil dbUtil = new DbUtil();

    @Override
    @SuppressWarnings("unchecked")
    public String showUserDetailedInfo(int userAccount) {
        String sql = "SELECT * FROM detailed_user WHERE user_account= ?";
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonString = new JSONObject();
        String[] params = {String.valueOf(userAccount)};
        List<DetailedUser> list = (List<DetailedUser>) dbUtil.selectContent(sql, params, DetailedUser.class);
        try {
            for (DetailedUser detailed : list) {
                jsonObject.put("user_account", detailed.getUser_account());
                jsonObject.put("user_nickname", detailed.getNickname());
                jsonObject.put("user_remark", detailed.getRemark());
                jsonObject.put("user_gender", detailed.getGender());
                jsonObject.put("user_phone", detailed.getPhone());
                jsonObject.put("user_email", detailed.getEmail());
                jsonObject.put("user_address", detailed.getAddress());
                jsonObject.put("user_question_numbers", detailed.getQuestion_numbers());
                jsonObject.put("user_question_true", detailed.getQuestion_true());
                jsonObject.put("user_question_errors", detailed.getQuestion_errors());
                jsonObject.put("user_question_rate", detailed.getQuestion_accuracy_rate());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonString.put("user_detailed", jsonObject);
        return jsonString.toString();
    }

    @Override
    public int setDefaultDetailedUserData(int userAccount) {
        //查询账号是否存在
        int status = 0;
        String sql = "select count(*) from detailed_user where user_account=?";
        String sql2 = "select count(*) from user_login where login_account=?";
        int count = DbUtil.selectCountIsExist(sql, new String[]{String.valueOf(userAccount)});
        int count2 = DbUtil.selectCountIsExist(sql2, new String[]{String.valueOf(userAccount)});
        //如果有登录信息
        if (count2 != 0) {
            //如果有详细信息
            if (count != 0) {
                return 0;
            } else {
                String setSql = "INSERT INTO `online_quiz_website`.`detailed_user`(`user_account`, `nickname`) VALUES ( ? , '用户')";
                status = DbUtil.updateContent(setSql, new String[]{String.valueOf(userAccount)});
            }
        }
        return status;
    }


    @Override
    public int editDetailedUserData(String userAccount, String param, String paramName) {
        int count;
        String sql = "UPDATE `online_quiz_website`.`detailed_user` SET "+paramName+" = ? WHERE  `user_account` = ?";
        String[] params = {param, String.valueOf(userAccount)};
        count = DbUtil.updateContent(sql, params);
        return count;
    }
}
